package edu.hcmus.sow.web.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.hcmus.sow.dao.GroupDAO;
import edu.hcmus.sow.dao.UserGroupDAO;
import edu.hcmus.sow.domain.Group;
import edu.hcmus.sow.domain.UserGroup;
import edu.hcmus.sow.service.UserService;
import edu.hcmus.sow.utils.SecurityUtil;

@Controller
@Transactional
public class UserGroupController {
   @Autowired
   private GroupDAO groupDAO;

   @Autowired
   private UserGroupDAO userGroupDAO;

   @Autowired
   private UserService userService;

   @RequestMapping("/admin/user/{id}/groups")
   @ResponseBody
   public List<Map<String, String>> getUserGroups(@PathVariable("id") int userId) {
      List<Map<String, String>> result = new ArrayList<Map<String, String>>();
      List<Group> groups = groupDAO.getGroups(SecurityUtil.getCurrentClient());
      List<UserGroup> userGroups = userGroupDAO.findByUserID(userId);

      for (Group group : groups) {
         Map<String, String> map = new HashMap<String, String>();
         map.put("id", group.getId().toString());
         map.put("name", group.getName());
         map.put("desc", group.getDescription());

         boolean checked = false;
         for (UserGroup userGroup : userGroups) {
            if (userGroup.getGroupID().intValue() == group.getId().intValue()) {
               checked = true;
               break;
            }
         }

         map.put("checked", String.valueOf(checked));
         result.add(map);
      }

      return result;
   }

   @RequestMapping(value = "/admin/user/{userId}/groups/update", method = RequestMethod.POST)
   @ResponseBody
   public void updateUserGroups(@PathVariable("userId") int userId,
         @RequestParam(value = "groupIds[]", required = false) int[] groupIds) {
      userService.updateUserGroups(userId, groupIds == null ? new int[0] : groupIds);
   }

   @RequestMapping("/admin/group/{id}/users")
   @ResponseBody
   public List<Map<String, String>> getUsersByGroup(@PathVariable("id") int groupId) {
      List<Map<String, String>> result = new ArrayList<Map<String, String>>();
      List<UserGroup> userGroups = userGroupDAO.findByGroup(groupId);

      for (UserGroup ug : userGroups) {
         Map<String, String> map = new HashMap<String, String>();
         map.put("id", ug.getUserID().toString());
         map.put("name", ug.getUser().getUsername());

         result.add(map);
      }

      return result;
   }

   @RequestMapping(value = "/admin/group/{groupId}/users/add", method = RequestMethod.POST)
   @ResponseBody
   public void addUsersToGroup(@PathVariable("groupId") int groupId,
         @RequestParam(value = "userIds[]", required = false) int[] userIds) {

      userService.addUsersGroup(groupId, userIds == null ? new int[0] : userIds);
   }

   @RequestMapping(value = "/admin/group/{groupId}/user/remove", method = RequestMethod.POST)
   @ResponseBody
   public void removeUserFromGroup(@PathVariable("groupId") int groupId, @RequestParam("userId") int userId) {

      userService.removeUserFromGroup(userId, groupId);
   }
}
